import Utils from "../common/Util";
import GameManager from "../common/GameManager";
import LangManager from "../i18n/LangManager";
import EventConst from "./EventConst";

// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export class PropPromptPanel extends cc.Component {
    /**显示title */
    @property(cc.RichText)
    title: cc.RichText;
    /**锤子取消 */
    @property(cc.Node)
    hammerBtn: cc.Node;
    /**手指取消 */
    @property(cc.Node)
    fingerBtn: cc.Node
    /**手指动画节点 */
    @property(cc.Node)
    guideFinger: cc.Node;
    /**锤子动画节点 */
    @property(cc.Node)
    hammerKnock: cc.Node;
    /**消除动画节点 */
    @property(cc.Node)
    grid: cc.Node;
    /**指头动画节点 */
    @property(cc.Node)
    fingerEffect: cc.Node;
    /**指头移动动画 */
    @property(cc.Node)
    fingerKnockEffect: cc.Node;
    private _type: PROP_TYPE;

    private _firstAni: cc.Animation;
    private _secAni: cc.Animation;
    private _lastAni: cc.Animation;
    start() {
        Utils.addEventListener(this.hammerBtn, this.cancleClick.bind(this));
        Utils.addEventListener(this.fingerBtn, this.cancleClick.bind(this));
    }

    onDestroy() {
        Utils.removeEventListener(this.hammerBtn);
        Utils.removeEventListener(this.fingerBtn);
        this._firstAni = null;
        this._secAni = null;
        this._lastAni = null;
    }

    public initData(type: PROP_TYPE) {
        this._type = type;
        this.hammerKnock.active = this.hammerBtn.parent.active = this.grid.active = this._type == PROP_TYPE.HAMMER;
        this.fingerKnockEffect.active = this.fingerBtn.parent.active = this.fingerEffect.active = this._type == PROP_TYPE.FINGER;
        this.title.string = `<b>${LangManager.getStrByKey(this._type == PROP_TYPE.HAMMER ? "prop_hammer" : "prop_magicFinger")}</b>`
        this.playAnimation();
    }
    /**播放动画 */
    private playAnimation() {
        if (!this.guideFinger) return;
        this._firstAni = this.guideFinger.getComponent(cc.Animation)
        if (this._type == PROP_TYPE.HAMMER) {
            this._lastAni = this.grid.getComponent(cc.Animation);
            this.grid.opacity = 255;
        } else {
            this.fingerEffect.children[1].opacity = this.fingerEffect.children[2].opacity = 0;//隐藏动画和16
            this.fingerEffect.children[0].opacity = 255;//显示8
            this._lastAni = this.fingerEffect.getComponent(cc.Animation);
        }
        this._firstAni.play();
        this._firstAni.on(cc.Animation.EventType.FINISHED, () => {
            this._firstAni.off(cc.Animation.EventType.FINISHED);
            this._secAni = this._type == PROP_TYPE.HAMMER ? this.hammerKnock.getComponent(cc.Animation) : this.fingerKnockEffect.getComponent(cc.Animation);
            let oldPos: cc.Vec2 = this._secAni.node.position;
            this._secAni.node.opacity = 255;
            this._secAni.play();
            this._secAni.on(cc.Animation.EventType.FINISHED, () => {
                this._secAni.play("alphatoZero")
                this._secAni.off(cc.Animation.EventType.FINISHED);
                this._lastAni.play();
                this._lastAni.on(cc.Animation.EventType.FINISHED, () => {
                    // this._secAni.node.setPosition(oldPos);
                    this._lastAni.off(cc.Animation.EventType.FINISHED);
                    let time = setTimeout(() => {
                        this && this.playAnimation();
                        clearTimeout(time);
                    }, 500)
                })
            })
        })
    }

    private cancleClick(evt) {
        // this.node.destroy();
        GameManager.getInstance().eventCenter.DispatchEvent(EventConst.PROP_CANCLE);
    }
}


export enum PROP_TYPE {
    HAMMER,
    FINGER
}